package router

import (
	"admin-system/internal/controller"
	"admin-system/internal/controller/auth_ctrl"
	"admin-system/internal/middleware"
	"github.com/gin-gonic/gin"
)

// InitializeRoutes 初始化所有路由
func InitializeRoutes(r *gin.Engine, ctrl *controller.Controllers, middleware *middleware.MiddlewareContainer) {
	// API版本分组
	v1 := r.Group("/api/v1")

	// 登录认证路由（重置中间件）
	registerAuthRouters(v1, ctrl.AdminUser)

	// 以下需要认证
	authGroup := v1.Group("/")
	// 加载中间件
	authGroup.Use(middleware.AuthMiddleware(), middleware.RBACMiddleware(), middleware.FixEmptyIDMiddleware())

	// 权限路由
	registerPermissionMenuRoutes(authGroup, ctrl)

}

// 登录认证路由组
func registerAuthRouters(v1 *gin.RouterGroup, Controller *auth_ctrl.AdminUserController) {
	auth := v1.Group("/auth")
	{
		auth.POST("/login", Controller.Login)
		auth.POST("/refresh-token", Controller.RefreshToken)
	}
}

// 权限路由组
func registerPermissionMenuRoutes(authGroup *gin.RouterGroup, ctl *controller.Controllers) {
	// 权限相关
	permissionCtl := ctl.Permission
	permissions := authGroup.Group("/permissions")
	{
		permissions.GET("/list", permissionCtl.GetPermissionList)
		permissions.GET("/list/:role_id", permissionCtl.GetPermissionList)
		permissions.POST("/create", permissionCtl.SavePermission)
		permissions.POST("/update", permissionCtl.SavePermission)
		permissions.POST("/delete/:id", permissionCtl.DelPermission)
	}
	// 菜单相关
	menuCtl := ctl.Menu
	menu := authGroup.Group("/menu")
	{
		menu.GET("/list", menuCtl.GetMenuList)
		menu.GET("/roles_menu", menuCtl.GetMenuListByRoles)
		menu.POST("/create", menuCtl.SaveMenu)
		menu.POST("/update", menuCtl.SaveMenu)
		menu.POST("/delete/:id", menuCtl.DeleteMenu)
	}
	//角色相关
	roleCtl := ctl.Role
	roles := authGroup.Group("/roles")
	{
		roles.GET("/list", roleCtl.GetRoleList)
	}

}
